package com.fourtwenty.hemptress.conf;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@XmlRootElement(name="config")
public class BotConfig {
	private static final Logger LOG = LoggerFactory.getLogger(BotConfig.class);
	
	private List<ServerChannel> sc = new ArrayList<>();
	private String commandPrefix = "!";
	
	@XmlElementWrapper(name="servers")
	@XmlElement(name="server")
	public List<ServerChannel> getSc() {
		return sc;
	}

	public void setSc(List<ServerChannel> sc) {
		this.sc = sc;
	}

	@XmlElement
	public String getCommandPrefix() {
		return commandPrefix;
	}

	public void setCommandPrefix(String commandPrefix) {
		this.commandPrefix = commandPrefix;
	}

	public BotConfig() {
		ServerChannel sc1 = new ServerChannel();
		sc1.setServerName("a");
		sc1.setChannels(new ArrayList<String>());
		sc1.getChannels().add("1");
		sc1.getChannels().add("2");
		
		ServerChannel sc2 = new ServerChannel();
		sc2.setServerName("b");
		sc2.setChannels(new ArrayList<String>());
		sc2.getChannels().add("1");
		sc2.getChannels().add("2");
		
		sc.add(sc1);
		sc.add(sc2);
	}
	
	public static void main(String[] args) {
		BotConfig bc = new BotConfig();
		marshal(new File("conf.xml"), bc);
	}
	
	public static void marshal(File file, BotConfig bc) {
		try {
			JAXBContext context = JAXBContext.newInstance(BotConfig.class);
			Marshaller m = context.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, (Boolean)true);
			m.marshal(bc, file);
		} catch (JAXBException e) {
			LOG.error("Could not marshall Bot config", e);
		}
		
	}
	public static BotConfig unmarshal(File file) {
		try {
			JAXBContext context = JAXBContext.newInstance(BotConfig.class);
			Unmarshaller um = context.createUnmarshaller();
			return (BotConfig)um.unmarshal(file);
		} catch(JAXBException e) {
			LOG.error("Could not create Bot configuration, returning default configuration.", e);
			return new BotConfig();
		}
	}
	
	public static class ServerChannel {
		private String serverName = "irc.freenode.net";
		private String port = "6667";
		private String user = "";
		private String password = "";
		private String nick = "Hemptress";
		private String ident = "HemptressBot 2.0";
		private List<String> channels;

		@XmlAttribute(name="address")
		public String getServerName() {
			return serverName;
		}

		public void setServerName(String serverName) {
			this.serverName = serverName;
		}

		@XmlAttribute(name="port")
		public String getPort() {
			return port;
		}

		public void setPort(String port) {
			this.port = port;
		}

		@XmlElement(name="channel")
		public List<String> getChannels() {
			return channels;
		}

		public void setChannels(List<String> channels) {
			this.channels = channels;
		}

		@XmlElement
		public String getUser() {
			return user;
		}

		
		public void setUser(String user) {
			this.user = user;
		}

		@XmlElement
		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

		@XmlElement
		public String getNick() {
			return nick;
		}

		public void setNick(String nick) {
			this.nick = nick;
		}

		@XmlElement
		public String getIdent() {
			return ident;
		}

		public void setIdent(String ident) {
			this.ident = ident;
		}
	
		
	}
}
